package net.lcheng.shop.Controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.sun.tools.javac.comp.Todo;
import net.lcheng.model.*;
import net.lcheng.service.*;
import net.lcheng.shop.vo.CurrentUserModel;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by zwr on 2016/6/28 0028.
 */
@Controller
public class Mycontroller {
    @Autowired
    private UserService userService;
    @Autowired
    private OOrderService oOrderService;
    @Autowired
    private OOrderRefundService oOrderRefundService;
    @Autowired
    private WxShopService shopService;
    @Autowired
    private TradeManageService tradeManageService;
    @Autowired
    private UserAgentService userAgentService;
    @RequestMapping("/my")
    public String myName(Model model) {
        Subject currentUser = SecurityUtils.getSubject();//获取当前用户
        if (currentUser.isAuthenticated()) {
            //获取用户
            Session session = currentUser.getSession();
            //String userId = session.getAttribute("userId").toString();
            CurrentUserModel currentUserModel = JSONObject.parseObject(session.getAttribute("user").toString(), CurrentUserModel.class);
            User user = userService.getUserByUserId(currentUserModel.getUserId());
            //如果userid为空的话就跳转登录页面
            if (user==null){
                SecurityUtils.getSubject().logout();
                return "redirect:/login";
            }
            model.addAttribute("user", user);
            /**查询该用户是否有店铺，如果有店铺，跳到卖家会员中心，如果没有就是普通用户（create by LV）**/
            if (user != null) {
                WxShop shop=shopService.getShopInforByUserId(user.getUserId());
                //判断用户有没有进行微店申请申请了就不显示这个按钮
                WxShop shopByUserId = shopService.getShopInforByUserId(currentUserModel.getUserId());
                if (shopByUserId==null){
                    model.addAttribute("shopByUserIdentity",1);
                }
                //判断用户有没有进行店铺申请申请了就消失申请按钮
                UserAgent userAgentByUserId=userAgentService.getUserAgentByUserId(user.getUserId());
                if (userAgentByUserId==null){
                    model.addAttribute("userAgentByUserIdentity",2);
                }
                if (shop!=null){
                    //获取待付款和待发货的订单数
                    int noPayment=tradeManageService.getOrderCountByOrderStatus(shop.getShopId(),0);
                    int havePayment=tradeManageService.getOrderCountByOrderStatus(shop.getShopId(),1);
                    model.addAttribute("noPayment",noPayment);
                    model.addAttribute("havePayment",havePayment);
                    model.addAttribute("shop",shop);
                    return "/my_index_seller";//卖家的会员中心
                }else {
                    //未支付(待支付)
                    List<OOrder> notPaid = oOrderService.getOOrderByBuyerUserId(currentUserModel.getUserId(), 0);
                    //已发货
                    List<OOrder> alreadyShipped = oOrderService.getOOrderByBuyerUserId(currentUserModel.getUserId(), 2);
                    //售后(退款)
                    List<OOrderRefund> oOrderRefunds = oOrderRefundService.getOOrderRefundBybuyerUserId(currentUserModel.getUserId());
                    model.addAttribute("oOrderRefunds", oOrderRefunds.size());
                    model.addAttribute("notPaidList", notPaid.size());
                    model.addAttribute("alreadyShippedList", alreadyShipped.size());
                    /**访问每个店铺，店铺的shopId需要带着，普通用户可以访问不同的店铺, create by LV**/ //Todo （暂时写死）
                    model.addAttribute("shopId",1);

                    return "my_index";
                }
            }
            return "redirect:/error";
        }
        return "redirect:/login";
    }
    //退出
    @RequestMapping(value = "/sign", method = RequestMethod.GET)
    public String logout(HttpServletRequest request) {
        String redirectUrl = request.getParameter("rec");
        SecurityUtils.getSubject().logout();
        if (StringUtils.isEmpty(redirectUrl)) {
            return "redirect:/login";
        } else {
            return "redirect:" + redirectUrl;
        }
    }
}
